<html>
<head>
    <meta charset="utf-8">
    <title>添加用户</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">用户名</label>
        <div class="layui-input-block">
            <input type="text" id="username" name="username" lay-verify="required" lay-reqtext="用户名不能为空"
                   placeholder="请输入用户名" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">密码</label>
        <div class="layui-input-block">
            <input type="password" id="password" name="password" lay-verify="required" lay-reqtext="密码不能为空"
                   placeholder="请输入密码" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">状态</label>
        <div class="layui-input-block">

            <input type="checkbox" id="status" name="status" lay-skin="switch" lay-text="启用|禁用">
        </div>
    </div>


    <!--    <div class="layui-form-item">-->
    <!--        <label class="layui-form-label required">用户权限</label>-->
    <!--        <div class="layui-input-block">-->

    <!--            <select id="roleid" name="roleid" lay-verify="required">-->
    <!--                <option value="">请选择用户权限</option>-->
    <!--                &lt;!&ndash;                <option value="1">管理员</option>&ndash;&gt;-->
    <!--                <option value="2">普通用户</option>-->
    <!--            </select>-->
    <!--        </div>-->
    <!--    </div>-->

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;
        $.ajaxSetup({
            beforeSend: function (xhr) {
// 假设你的 token 存储在 localStorage 中
                var token = localStorage.getItem('token');
                if (token) {
// 将 token 添加到请求头中
                    xhr.setRequestHeader('Authorization', 'Bearer ' + token);
                }
            }
        });
        const queryString = window.location.search;
        const queryParams = new URLSearchParams(queryString);
        const id = queryParams.get('id');
        if (id) {//编辑
            //通过id获取数据
            $.ajax({
                url: "/api/admin/user?action=get&id=" + id,//访问的链接
                type: 'get',//提交方式 常用的是get或者post
                dataType: "json",//返回的数据类型
                // async: false,//是否异步加载
                success: function (res) {//访问成功返回的方法
                    if (res.success) {
                        $("#username").val(res.data.username)//设置id为depname的数据框的value属性为xxxx
                        $("#password").val(res.data.password)
                        if (res.data.status === true) {
                            $("#status").attr("checked", "checked") //.attr(XX1,XX2)给元素添加xx1属性为XX2
                        }
                        form.render(); //更新全部
                    }

                },
                error: function (err) {//访问失败返回的方法
                    //  debugger
                }
            })

        }

        //监听提交
        form.on('submit(saveBtn)', function (data) {

            const status = data.field.status === "on";
            let url = "/api/admin/user?action=add&username=" + data.field.username + "&password=" + data.field.password
                + "&status=" + status;
            //添加
            if (id) {//编辑
                url = "/api/admin/user?action=edit&id=" + id + "&username=" + data.field.username + "&password=" + data.field.password
                    + "&status=" + status
            }

            $.ajax({
                url: url,//访问的链接
                type: 'get',//提交方式 常用的是get或者post
                dataType: "json",//返回的数据类型
                // async: false,//是否异步加载
                success: function (res) {//访问成功返回的方法
                    // debugger
                    layer.msg(res.message);
                    setTimeout(function () {
                        //执行搜索重载
                        parent.layui.table.reload('currentTableId', {
                            page: {
                                curr: 1
                            }
                        }, 'data');
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);

                    }, 1000)

                },
                error: function (err) {//访问失败返回的方法
                    //  debugger
                }
            })

            return false;
        });

    });
</script>
</body>
</html>
